﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView
{
    public partial class BianZhiCheckWindow : ChildWindow
    {
        private int OrgID = 0;
        private string Type = "";
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private ObservableCollection<EmpbasicService.BianZhiModel> SelectItems = new ObservableCollection<EmpbasicService.BianZhiModel>();
        private EmpbasicService.BianZhiModel model = new EmpbasicService.BianZhiModel();
        public BianZhiCheckWindow(int orgID, string type)
        {
            InitializeComponent();
            Type = type;
            if (type == "乡科级正职核定数")
            {
                xingzheng.Visibility = System.Windows.Visibility.Visible;
                zhengfa.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                xingzheng.Visibility = System.Windows.Visibility.Collapsed;
                zhengfa.Visibility = System.Windows.Visibility.Visible;
            }
            OrgID = orgID;
            Client.CanTCXZCompleted += new EventHandler<EmpbasicService.CanTCXZCompletedEventArgs>(Client_CanTCXZCompleted);
            Client.TCXZCompleted += new EventHandler<EmpbasicService.TCXZCompletedEventArgs>(Client_TCXZCompleted);
            Client.GetBianZhiModelForTongChouCompleted +=
                    new EventHandler<EmpbasicService.GetBianZhiModelForTongChouCompletedEventArgs>(Client_GetBianZhiModelForTongChouCompleted);
            Client.GetBianZhiModelForTongChouAsync(OrgID);
            this.DataContext = model;
        }



        void Client_GetBianZhiModelForTongChouCompleted(object sender, EmpbasicService.GetBianZhiModelForTongChouCompletedEventArgs e)
        {
            lb.ItemsSource = null;
            lb.ItemsSource = e.Result;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            SelectItems = null;
            var source = lb.ItemsSource as System.Collections.ObjectModel.ObservableCollection<EmpbasicService.BianZhiModel>;
            if (source != null)
                SelectItems = new ObservableCollection<EmpbasicService.BianZhiModel>(source.Where(c => c.IsSelected == true));
            if (SelectItems != null && SelectItems.Count > 0)
            {
                string ids = "";
                foreach (EmpbasicService.BianZhiModel i in SelectItems)
                {
                    ids += i.ID.ToString() + ",";
                }
                ids = ids.Substring(0, ids.Length - 1);
                Client.CanTCXZAsync(Type, ids);
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要统筹的内设科室！");
        }

        void Client_CanTCXZCompleted(object sender, EmpbasicService.CanTCXZCompletedEventArgs e)
        {
            if (e.Result == false)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("所选岗位已进行统筹安排！");
                return;
            }
            if (string.IsNullOrEmpty(model.ZhengKeLingDao))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请填写要统筹的职数！");
                return;
            }

            if (MessageBox.Show("确定要为选择的内设科室进行统筹吗？", "系统提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                string ids = "";
                foreach (EmpbasicService.BianZhiModel i in SelectItems)
                {
                    ids += i.ID.ToString() + ",";
                }
                ids = ids.Substring(0, ids.Length - 1);
                Client.TCXZAsync(Type, Convert.ToInt32(model.ZhengKeLingDao), ids, Type == "乡科级正职核定数" ? model.TCMC : model.TCMC1);

            }
        }

        void Client_TCXZCompleted(object sender, EmpbasicService.TCXZCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("统筹成功！");
            this.DialogResult = true;
        }

        private string pattern = @"^[0-9]+([.]{1}[0-9]+){0,1}$";
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            Match m = Regex.Match(((TextBox)sender).Text, pattern); // 匹配正则表达式
            if (!m.Success) // 输入的不是数字
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("只能输入数字！");
                ((TextBox)sender).Text = "";
                return;
            }
        }
    }
}
